# -*- coding: utf-8 -*-
# @Time : 2024/1/11 7:41 PM 
# @Author : Wayne
# @Email : 1
# @File : time_diff_type_add.py
# @Project : young_python_demo
# @DESCRIPTION : 不同时间规则相加减

"""
此方法太过复杂了，暂时不建议使用，可以根据自己需求适当摘取相关方法处理
"""


# 日期时间计算函数（支持天、小时、分钟、秒），start_time起始日期，duration时长，time_unit时间单位(天：days、小时：hours，分钟：minutes，秒：seconds)
def add_time(start_time, duration, time_unit):
    from datetime import datetime, timedelta
    start_date = datetime.strptime(start_time, "%Y-%m-%d %H:%M:%S")
    if time_unit == "days":
        new_date = start_date + timedelta(days=duration)
    elif time_unit == "hours":
        new_date = start_date + timedelta(hours=duration)
    elif time_unit == "minutes":
        new_date = start_date + timedelta(minutes=duration)
    elif time_unit == "seconds":
        new_date = start_date + timedelta(seconds=duration)
    else:
        return "Invalid time unit!"
    new_time = new_date.strftime("%Y-%m-%d %H:%M:%S")
    return new_time
# 日期时间计算函数（支持年、月、天、小时、分钟、秒），start_time起始日期，duration时长，time_unit时间单位(年：years，月：months，天：days、小时：hours，分钟：minutes，秒：seconds)
def add_time(start_time, duration, time_unit):
    from datetime import datetime, timedelta
    start_date = datetime.strptime(start_time, "%Y-%m-%d %H:%M:%S")
    if time_unit == "years":
        new_date = start_date + timedelta(days=duration * 365)
    elif time_unit == "months":
        new_date = start_date
        year = new_date.year
        month = new_date.month
        for _ in range(duration):
            month += 1
            if month > 12:
                month = 1
                year += 1
            if start_date.day > 28:
                # Handling February leap year edge case
                while True:
                    try:
                        new_date = new_date.replace(year=year, month=month, day=start_date.day)
                    except ValueError:
                        start_date = start_date.replace(day=start_date.day - 1)
                    else:
                        break
            else:
                new_date = new_date.replace(year=year, month=month)
    elif time_unit == "days":
        new_date = start_date + timedelta(days=duration)
    elif time_unit == "hours":
        new_date = start_date + timedelta(hours=duration)
    elif time_unit == "minutes":
        new_date = start_date + timedelta(minutes=duration)
    elif time_unit == "seconds":
        new_date = start_date + timedelta(seconds=duration)
    else:
        return "Invalid time unit!"
    new_time = new_date.strftime("%Y-%m-%d %H:%M:%S")
    return new_time




from datetime import datetime, timedelta

"""
获取当月最后一天
"""
def get_last_day_of_month(date_str):
    # 将输入的日期字符串转换为datetime对象
    date_obj = datetime.strptime(date_str, "%Y-%m-%d")
    # 下个月的第一天减去一天就是这个月的最后一天
    last_day_of_month = date_obj.replace(day=1, month=date_obj.month + 1) - timedelta(days=1)
    return last_day_of_month.strftime("%Y-%m-%d")  # 返回格式化的日期字符串


# 示例
date_input = "2024-01-08"
print(get_last_day_of_month(date_input))  # 应该输出2024-05-31，即2024年5月的最后一天

from datetime import datetime, timedelta

"""
获取下个月第一一天
"""
def get_first_day_of_next_month(date_str):
    # 将输入的日期字符串转换为datetime对象
    date_obj = datetime.strptime(date_str, "%Y-%m-%d")

    # 下个月的第一天
    first_day_of_next_month = date_obj.replace(day=1, month=date_obj.month + 1)

    return first_day_of_next_month.strftime("%Y-%m-%d")  # 返回格式化的日期字符串


# 示例
date_input = "2024-01-08"
print(get_first_day_of_next_month(date_input))  # 应该输出2024-06-01，即2024年6月的第一天
